﻿<?php
define('SMTP_INCLUDED', 1);

class SendMail {
	var $smtpHost;  // Dia chi mail server
	var $mailName;  // Ten mail
	var $smtpUser;  // User duoc khai bao tren mail server
	var $smtpPass;  // Pass cua email nay

	public function SendMail() {
		$this->smtpHost = 'www.tracviet.vn';
		$this->mailName = 'info@tracviet.vn';
		$this->smtpUser = 'info@tracviet.vn';
		$this->smtpPass = '1234567t';
	}

	public function Send($subject, $body, $information = array(), $to, $cc = array(), $bcc = array(), $userName = "Admin") {
		$fromMail = $this->mailName;
		$from = $userName . " <" . $fromMail . ">";
		$headers = "Return-Path: " . $userName . " <" . $fromMail . ">\r\n";
		$headers .= "From: $from\nX-Mailer: " . $userName . "\r\n";
		$headers .= "Mime-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=utf-8\r\n";
		$message = "<p>";
		if (count ($information) > 0) {
			$message .= "<fieldset style='border:1px solid #afe14c;margin: 7px 0;padding: 10px 10px'>";
			$message .= "<legend style='font: 700 14px Arial, Helvetica, sans-serif;padding: 0 5px;margin: 0 10px;color: #73b304'>Thông tin</legend>";
			foreach ($information as $key => $value) {
				$message .= "+ " . $key . " : <b>" . $value . "</b><br>";
			}
			$message .= "</fieldset>";
		}
		$message .= "<fieldset style='border:1px solid #afe14c;margin: 7px 0;padding: 10px 10px'>";
		$message .= "<legend style='font: 700 14px Arial, Helvetica, sans-serif;padding: 0 5px;margin: 0 10px;color: #73b304'>Nội dung</legend>";
		$message .= $body;
		$message .= "</fieldset></p>";
		$message = eregi_replace("[\]",'',$message);
		
		$result  = $this->smtpMail($subject, $message, $headers, $to, $cc, $bcc);
		return $result;
	}

	public function smtpMail($subject, $message, $headers = '', $mailTo, $cc = array(), $bcc = array()) {
		$message = preg_replace("/(?<!\r)\n/si", "\r\n", $message);
		if ($headers != "") {
			if(is_array($headers)) {
				if(sizeof($headers) > 1) {
					$headers = join("\r\n", $headers);
				} else {
					$headers = $headers[0];
				}
			}
			$headers = chop($headers);
		}
		
		if (trim($mailTo) == "") {
			exit();
		}
		if (trim($subject) == "") {
			die("No email Subject specified");
		}
		
		if (!is_array($mailTo)) {
			$mailTo = explode(",", $mailTo);
		}
		
		// Ok we have error checked as much as we can to this point let's get on
		// it already.
		if( !$socket = fsockopen($this->smtpHost, 25, $errno, $errstr, 20)) {
			die("Could not connect to smtp host : $errno : $errstr");
		}
		$this->serverParse($socket, "220");

		if (!empty($this->smtpUser) && !empty($this->smtpPass)) {
			// Send the RFC2554 specified EHLO.
			// This improved as provided by SirSir to accomodate
			// both SMTP AND ESMTP capable servers
			fputs($socket, "EHLO " . $this->smtpHost . "\r\n");
			$this->serverParse($socket, "250");

			fputs($socket, "AUTH LOGIN\r\n");
			$this->serverParse($socket, "334");
			fputs($socket, base64_encode($this->smtpUser) . "\r\n");
			$this->serverParse($socket, "334");
			fputs($socket, base64_encode($this->smtpPass) . "\r\n");
			$this->serverParse($socket, "235");
		} else {
			// Send the RFC821 specified HELO.
			fputs($socket, "HELO " . $this->smtpHost . "\r\n");
			$this->serverParse($socket, "250");
		}

		// From this point onward most server response codes should be 250
		// Specify who the mail is from....
		fputs($socket, "MAIL FROM: <" . $this->mailName . ">\r\n");
		$this->serverParse($socket, "250");

		// Send to all by array mail
		if (count($mailTo) > 0) {
			$to_header = "To: ";
			@reset($mailTo);
			while(list( , $mail_to_address ) = each($mailTo)) {
				// Add an additional bit of error checking to the To field.
				$mail_to_address = trim($mail_to_address);
				if (preg_match('/[^ ]+\@[^ ]+/', $mail_to_address)) {
					fputs( $socket, "RCPT TO: <$mail_to_address>\r\n");
					$this->serverParse( $socket, "250" );
				}
				$to_header .= "<$mail_to_address>, ";
			}
		}
		
		// Send to Bcc:
		if (count ($bcc) > 0) {
			@reset($bcc);
			while (list( , $bcc_address) = each($bcc)) {
				// Add an additional bit of error checking to bcc header...
				$bcc_address = trim($bcc_address );
				if ( preg_match('/[^ ]+\@[^ ]+/', $bcc_address)) {
					fputs($socket, "RCPT TO: <$bcc_address>\r\n");
					$this->serverParse($socket, "250");
				}
			}
		}
		
		// Send to Cc:
		if (count($cc) > 0) {
			@reset($cc);
			while(list( , $cc_address) = each($cc)) {
				// Add an additional bit of error checking to cc header
				$cc_address = trim($cc_address);
				if (preg_match('/[^ ]+\@[^ ]+/', $cc_address)) {
					fputs($socket, "RCPT TO: <$cc_address>\r\n");
					$this->serverParse($socket, "250");
				}
			}
		}
		
		// Ok now we tell the server we are ready to start sending data
		fputs($socket, "DATA\r\n");

		// This is the last response code we look for until the end of the message.
		$this->serverParse($socket, "354");

		// Send the Subject Line...
		fputs($socket, "Subject: $subject\r\n");

		// Now the To Header.
		fputs($socket, "$to_header\r\n");

		// Now any custom headers....
		fputs($socket, "$headers\r\n\r\n");

		// Ok now we are ready for the message...
		fputs($socket, "$message\r\n");

		// Ok the all the ingredients are mixed in let's cook this puppy...
		fputs($socket, ".\r\n");
		$this->serverParse($socket, "250");

		// Now tell the server we are done and close the socket...
		fputs($socket, "QUIT\r\n");
		fclose($socket);
		return true;
	}
	
	public function serverParse($socket, $response) {
	   $server_response = '';
	   while ( substr($server_response, 3, 1) != ' ') {
		  if (!( $server_response = fgets($socket, 256))) {
			 echo "Couldn't get mail server response codes";
		  }
	   }

	   if (!( substr($server_response, 0, 3) == $response)) {
		  echo "Ran into problems sending Mail. Response: $server_response";
	   }
	}	
}
?>